<?php
/**
 * 《编号管理》实现类
 *
 * @author 孙晓晔
 * @version $Id: Code.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/InfoUtil.class.php');

class Code extends InfoUtil {
	public $tbl = Constant::tbl_code;

	public $category = array(
		'-' => '请选择',
		'1' => '民族',
		'2' => '工件',
		'3' => '工艺',
		'4' => '省份',
		'5' => '分类',
	);

	public $orderby = array(
		'-' => '请选择',
		'code' => '编号',
		'name' => '名称',
		'orderby' => '排序',
	
		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}
	
	function mobile($controller, $request) {
		$this->pretreat($controller);
	
		global $CategoryTree;
	
		$data = $CategoryTree['系统管理'];
	
		$ret = array();
		foreach ($data as $k => $v) {
			if ($this->tree_my($v)) {
				$ret[$k] = $v;
			}
		}
	
		$controller->assign('menu', $ret);
	
		$controller->display($request, 'mobile');
		return true;
	}
	
	function tab($controller, $request) {		
		$data = array(
			'编号信息' => array('cc'=>'info.php?do=Code.find', 'js'=>''),
			'部门信息' => array('cc'=>'info.php?do=Dept.find', 'js'=>'1'),
			
			'工作中心' => array('cc'=>'info.php?do=Work.find', 'js'=>''),
		
			'国家信息' => array('cc'=>'info.php?do=Country.find', 'js'=>''),	
			'国内城市' => array('cc'=>'info.php?do=Zone.find', 'js'=>''),
			'国内区县' => array('cc'=>'info.php?do=District.find', 'js'=>''),
			'区县编号' => array('cc'=>'info.php?do=Region.find', 'js'=>''),
				
			'统计分析' => array('cc'=>'info.php?do=Code.tj_tab', 'js'=>''),
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);
		
		$controller->display($request, 'tab');
		return true;
	}
	
	function tj_tab($controller, $request) {		
		$data = array(
			'操作日志' => array('cc'=>'log.php?do=LogAction.group', 'js'=>''),
			'网络流量' => array('cc'=>'log.php?do=Network.group', 'js'=>''),
			'上网日志' => array('cc'=>'log.php?do=Layer7.group', 'js'=>''),
		
			// '网络信息' => array('cc'=>'log.php?do=Iptable.group', 'js'=>''),
			'语音网关' => array('cc'=>'emp.php?do=Sip.group', 'js'=>''),				
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tj_tab');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'info/code/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$category_s = $request['category_s'];
		$name_s = $request['name_s'];

		$where = " WHERE 1=1";

		if (!empty($category_s) && $category_s <> '-') {
			$where .= " AND category = '" . $category_s . "'";
		}

		if (!empty($name_s)) {
			$where .= " AND name = '" . $name_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);				

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'info/code/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);
		
		$controller->assign_by_ref('sequence_option', range(0, 12, 1));

		$controller->display($request, 'info/code/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$category = $request['category'];
		$code = $request['code'];
		$name = $request['name'];

		$orderby = $request['orderby'];

		$status = $request['status'];

		clean("code_" . $category . "_status_" . $status);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(category, code, name, orderby";
		$fmt .= ", status, createtime, modifytime) VALUES('%s', '%s', '%s'";
		$fmt .= ", '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $category, $code, $name, $orderby, $status);
		// debug($sql);
		$db->query($sql);
		
		$request['id_s'] = $db->insert_id();

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);
		
		$controller->assign_by_ref('sequence_option', range(0, 12, 1));

		$controller->display($request, 'info/code/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$category = $request['category'];
		$code = $request['code'];
		$name = $request['name'];

		$orderby = $request['orderby'];

		$status = $request['status'];

		clean("code_" . $category . "_status_" . $status);

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET category = '%s', code = '%s', name = '%s', orderby = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $category, $code, $name, $orderby, $status, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function copy($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);
		
		$controller->assign_by_ref('sequence_option', range(0, 12, 1));

		$controller->display($request, 'info/code/copy');
		return true;
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'info/code/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['category_t'] = $this->category[$row['category']];
		
		$row = $this->_view_t($row);

		return $row;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function importPost($controller, $request) {
		$this->pretreat($controller);

		$data = $request['data'];
		// echo $data;

		$aa = preg_split("/[\\n]+/i", $data);
		// print_r($a);

		$count = 1;
		$category = 1;
		$db = newdb($this->dsn);
		while (list($k, $v) = each($aa)) {
			$buf = trim($v);
			if (empty($buf)) {
				continue;
			}

			if (preg_match("/^\d+$/", $buf)) {
				$category = $buf;
				continue;
			}

			$code = $count;
			$name = trim($buf);

			$fmt = "INSERT INTO %s(category, `code`, name) VALUES('%s', '%s', '%s')";
			$sql = sprintf($fmt, $this->tbl, $category, $code, $name);
			// debug($sql);
			$db->query($sql);

			$count += 3;
		}

		$this->findPost($controller, $request);
		return true;
	}
}
?>